package com.shujia.day14;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/*
    "aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:
    "a(5)b(4)c(3)d(2)e(1)"

 */
public class TreeMapTest1 {
    public static void main(String[] args) {
        String s1 = "aababcabcdabcde";

        TreeMap<Character, Integer> map1 = new TreeMap<>();

        //将字符串转字符数组
        char[] chars = s1.toCharArray();

        //遍历字符数组向集合中添加
        for (char c : chars) {
            if (map1.containsKey(c)) {
                map1.put(c, map1.get(c) + 1);
            } else {
                map1.put(c, 1);
            }
        }

        StringBuilder sb = new StringBuilder();
        Set<Map.Entry<Character, Integer>> entries = map1.entrySet();
        for (Map.Entry<Character, Integer> entry : entries) {
            Character c = entry.getKey();
            Integer count = entry.getValue();
            sb.append(c).append("(").append(count).append(")");
        }

        String resStr = sb.toString();
        System.out.println("结果为：" + resStr);


    }
}
